python-stdlib/enum/enum.py: Add Enum class.#980
python-stdlib/enum/enum.py: Add Enum class.#980IhorNehrutsa wants to merge 3 commits intomicropython:masterfrom
Conversation
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
|
Usage example:: Output is:: |
|
Thanks for the contribution, this looks pretty good! Did you implement this from scratch, or copy parts from CPython's implementation? I'm just wondering about licensing and copyright. Can you please add the test to the CI, in |
I just saw CPython Enum. It looks like incredible magic. :-) |
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
That doesn't really answer the question. Did you copy this implementation from CPython? Also, please make sure the CI all passes, there's currently a failure. |
|
| Did you implement this from scratch, or copy parts from CPython's implementation? No, I didn't use CPython implementation. It was inspired by @shariltumin Dot class from the Way to use dot notation to refer to states in a state machine #15694 |
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
|
Should I squash commits? |
| @@ -0,0 +1,91 @@ | |||
| # enum_test.py | |||
|
|
|||
| from enum import Enum, enum | |||
There was a problem hiding this comment.
I tried to run this test under CPython 3.12.2 but it doesn't work, for many reasons. And it should run under CPython so we can test that the implementation of MicroPython's enum matches the CPython enum.
For example, enum does not exist in the enum CPython module. Which version of CPython were you testing against?
| Enabled = True | ||
|
|
||
|
|
||
| state = Enum() |
There was a problem hiding this comment.
CPython cannot create enums in this way.
|
|
||
| state = Enum() | ||
| print(state) | ||
| state = Direction() |
There was a problem hiding this comment.
CPython requires a value in the constructor here.
| print(state) | ||
| state = State() | ||
| print(state) | ||
| state = State({"X": 1.0, "Y": 2.0}) |
There was a problem hiding this comment.
CPython doesn't allow such an argument to the constructor.
|
|
||
| print("Direction(Direction.CCW):", Direction(Direction.CCW)) | ||
| print("Direction('CW'):", Direction("CW")) | ||
| print("state(10):", state(10)) |
There was a problem hiding this comment.
CPython doesn't allow calling an enum.
| print("state('CW'):", state("CW")) | ||
| print("type(state('CW')):", type(state("CW"))) | ||
|
|
||
| print("state.key_from_value(20):", state.key_from_value(20)) |
There was a problem hiding this comment.
CPython doesn't have key_from_value().
| CCW = "CCW" | ||
|
|
||
|
|
||
| class State(Direction): |
There was a problem hiding this comment.
CPython doesn't allow inheriting enums from each other.
| print("type(state('CW')):", type(state("CW"))) | ||
|
|
||
| print("state.key_from_value(20):", state.key_from_value(20)) | ||
| print("len(state):", len(state)) |
There was a problem hiding this comment.
CPython doesn't have __len__ on an enum.
|
|
||
| print("state.keys():", state.keys()) | ||
| print("state.values():", state.values()) | ||
| print("state.items():", state.items()) |
There was a problem hiding this comment.
CPython enums don't have keys/values/items methods.
|
There is a quite comprehensive set of unit-tests for enum available in CPython: https://github.com/python/cpython/blob/main/Lib/test/test_enum.py |
|
I have successfully completed the task that requires the Enum class. |
We need to mip install `datetime`, tzif-parser requires it. https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime ``` mpremote connect id:a5f14b8bfbff4289 mip install datetime ``` ## CPython features originally used in `tzif-parser` which were missing or different in MicroPython * `sysconfig` * `enum` - micropython/micropython-lib#269 - micropython/micropython-lib#980 * Regex _named_ Groups - https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups - https://docs.micropython.org/en/latest/library/re.html * `dataclasses` - https://docs.python.org/3/library/dataclasses.html - https://github.com/orgs/micropython/discussions/13741 - https://github.com/dhrosa/udataclasses - https://udataclasses.readthedocs.io/en/latest/ * `struct` (https://docs.python.org/3/library/struct.html#format-characters) features not available in MicroPython (https://docs.micropython.org/en/latest/library/struct.html#module-struct): - `c` for `char`, which in CPython would be decoded as an instance of [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes-objects) with length 1. Instead of `c`, we could use `s`, but we know this is a single byte, so `b` does just fine, giving us a Python integer. The integral repeat count prefix `1` is superfluous. * [MicroPython `bytearray`](https://www.fredscave.com/43-micropython-data-types-bytearray.html) type doesn't have the [`clear()`](https://docs.python.org/3/library/stdtypes.html#sequence.clear) method [mutable sequences](https://docs.python.org/3/library/stdtypes.html#typesseq-mutable) in CPython do * `Typing.IO` - python/typing#829
We need to mip install `datetime`, tzif-parser requires it. https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime ``` mpremote connect id:a5f14b8bfbff4289 mip install datetime ``` ## CPython features originally used in `tzif-parser` which were missing or different in MicroPython * `sysconfig` * `enum` - micropython/micropython-lib#269 - micropython/micropython-lib#980 * Regex _named_ Groups - https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups - https://docs.micropython.org/en/latest/library/re.html * `dataclasses` - https://docs.python.org/3/library/dataclasses.html - https://github.com/orgs/micropython/discussions/13741 - https://github.com/dhrosa/udataclasses - https://udataclasses.readthedocs.io/en/latest/ * `struct` (https://docs.python.org/3/library/struct.html#format-characters) features not available in MicroPython (https://docs.micropython.org/en/latest/library/struct.html#module-struct): - `c` for `char`, which in CPython would be decoded as an instance of [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes-objects) with length 1. Instead of `c`, we could use `s`, but we know this is a single byte, so `b` does just fine, giving us a Python integer. The integral repeat count prefix `1` is superfluous. * [MicroPython `bytearray`](https://www.fredscave.com/43-micropython-data-types-bytearray.html) type doesn't have the [`clear()`](https://docs.python.org/3/library/stdtypes.html#sequence.clear) method [mutable sequences](https://docs.python.org/3/library/stdtypes.html#typesseq-mutable) in CPython do * `Typing.IO` - python/typing#829
Docs in:
docs/library/enum.rst: Add Enum class. #16842
Usage example:
Output is:
EDITED:
Inspired by @shariltumin Dot class from the Way to use dot notation to refer to states in a state machine #15694
and @njourdane enum() func from the Request for package: micropython-enum #269